<!--
 * @Author: your name
 * @Date: 2021-08-19 15:25:06
 * @LastEditTime: 2021-12-09 19:51:39
 * @LastEditors: wei_jt@ecidi.com
 * @Description: In User Settings Edit
 * @FilePath: \central-system\src\components\PdfSignature\index.vue
-->
<template>
  <fks-dialog
    :visible="visible"
    :close-on-click-modal="false"
    :modal="true"
    :append-to-body="true"
    @close="close"
  >
    <iframe
      id="ifr"
      ref="iframe"
      class="page"
      name="iframes"
      src="./static/pdfjs/viewer.html"
    />
    <div slot="footer">
      <fks-button type="primary" icon="fks-icon-check" @click="submit">
        <span style="margin-left: 4px">{{ LANG.SAVE }}</span>
      </fks-button>
    </div>
  </fks-dialog>
</template>
<script>
import { updateFile, deleteFile, uploadFile } from './api'
import { getSign } from '@/utils/request/sign'
import { download } from '@/utils/file'

import qs from 'qs'
export default {
  name: 'PdfSignature',
  props: {
    fileToken: String,
    keySn: String
  },
  data() {
    return {
      iframeData: null,
      visible: false,
      jinges: '',
      pdfSrc: '',
      deleteFlag: false,
      pdfURL: ''
    }
  },

  watch: {},
  methods: {
    open(documentId) {
      const KGPdfViewerWebApp = this.iframeData.KGPdfViewerWebApp
      KGPdfViewerWebApp.open(documentId)
    },
    // stamp() {
    //   const KGPdfViewerWebApp = this.iframeData.KGPdfViewerWebApp
    //   KGPdfViewerWebApp.stamp()
    // },
    // stampOfText(text) {
    //   const KGPdfViewerWebApp = this.iframeData.KGPdfViewerWebApp
    //   KGPdfViewerWebApp.stampOfText(
    //     `1-${KGPdfViewerWebApp.PDFViewerApplication.pagesCount}`,
    //     text
    //   )
    // },
    // stampOfXY(left, top) {
    //   const KGPdfViewerWebApp = this.iframeData.KGPdfViewerWebApp
    //   KGPdfViewerWebApp.stampOfXY(
    //     `1-${KGPdfViewerWebApp.PDFViewerApplication.pagesCount}`,
    //     left,
    //     top
    //   )
    // },
    setConfig(fileToken, keySn) {
      let href = window.location.href
      let hash = window.location.hash
      let baseUrl = href.replace(hash, '')
      let signatureApi = this.VUE_APP_SIGNATURE_API
      while (
        signatureApi.indexOf('./') === 0 ||
        signatureApi.indexOf('../') === 0
      ) {
        //到达根级时跳出
        // if (baseUrl == orgin) break

        if (signatureApi.indexOf('./') === 0) {
          let index = baseUrl.lastIndexOf('/')
          baseUrl = baseUrl.substring(0, index)

          signatureApi = signatureApi.replace('./', '')
        }
        if (signatureApi.indexOf('../') === 0) {
          let index = baseUrl.lastIndexOf('/')
          baseUrl = baseUrl.substring(0, index)

          //到达根级时跳出
          // if (baseUrl == orgin) break

          index = baseUrl.lastIndexOf('/')
          baseUrl = baseUrl.substring(0, index)

          signatureApi = signatureApi.replace('../', '')
        }
      }
      let signatureUrl = baseUrl + '/' + signatureApi
      this.iframeData = this.$refs.iframe.contentWindow
      const KGPdfViewerWebApp = this.iframeData.KGPdfViewerWebApp
      KGPdfViewerWebApp.setUrl(signatureUrl)
      KGPdfViewerWebApp.userId = keySn
      KGPdfViewerWebApp.sealType = 'PT'
      KGPdfViewerWebApp.stampType = 0
      KGPdfViewerWebApp.sealOrigin = 0
      KGPdfViewerWebApp.certOrigin = 0
      KGPdfViewerWebApp.fileType = 'PDF'
      KGPdfViewerWebApp.stampSuccess = () => {}
      KGPdfViewerWebApp.deleteSealSuccess = () => {}
      this.open(fileToken + '.pdf')
    },
    downloadPdf(fileToken, keySn) {
      if (fileToken) {
        this.visible = true
        uploadFile({ fileToken: fileToken })
          .then((res) => {
            if (res.status) {
              this.visible = true
              setTimeout(() => {
                this.setConfig(fileToken, keySn)
              }, 500)
            } else {
              this.$message.error('上传文件失败')
            }
          })
          .catch((error) => {
            this.$message.error('上传文件失败')
          })
      }
    },
    close() {
      if (!this.deleteFlag) {
        deleteFile({ fileToken: this.fileToken })
        this.visible = false
      } else {
        this.visible = false
      }
    },
    submit() {
      this.deleteFlag = true
      updateFile({ fileToken: this.fileToken })
        .then((res) => {
          if (res.status) {
            this.$emit('update:fileToken', res.data.fileToken)
            this.$message.success('保存签章文件成功')
          } else {
            this.$message.error('保存签章失败')
          }
        })
        .catch((error) => {
          this.$message.error('保存签章失败')
        })
      this.visible = false
    }
    // sendMessage(data) {
    //   this.$nextTick(function () {
    //     const jinges = this.$refs.jinge
    //     if (jinges.attachEvent) {
    //       // 兼容浏览器判断
    //       jinges.attachEvent('onload', function () {
    //         const iframeWin = jinges.contentWindow
    //         iframeWin.postMessage(data, '*')
    //       })
    //     } else {
    //       jinges.onload = function () {
    //         const iframeWin = jinges.contentWindow
    //         iframeWin.postMessage(data, '*')
    //       }
    //     }
    //   })
    // }
  }
}
</script>

<style lang="scss" scoped>
.page {
  display: flex;
  width: 100%;
  height: 800px;
  overflow: auto;
}

::v-deep .fks-dialog__body {
  padding-top: 0 !important;
  padding-bottom: 0 !important;
}

::v-deep .fks-dialog .fks-dialog__header {
  height: 57px !important;
}

::v-deep .fks-dialog__footer {
  padding-top: 12px;
  padding-bottom: 12px;
}

body::-webkit-scrollbar {
  display: none;
}
</style>
